# -*- coding:utf-8 -*-
from __future__ import absolute_import

import sys, os
from datetime import datetime

from sqlalchemy.orm import relationship, backref
from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy import Column, ForeignKey, Integer, String, Date, DateTime, Float

sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), "../..")))

from exchange.db.mysql import mysql, model_base
base = declarative_base(cls=model_base, bind=mysql().gen_engine())

class model:

    def clear():
        base.metadata.drop_all()

    def init():
        base.metadata.create_all()

    def load_base_data():
        pass




'''
account
'''
class model_account(base):

    __tablename__ = 'account'                                                   ### 账户 ###

    id          = Column(Integer, primary_key=True, autoincrement=True)         # 主键
    create_time = Column(DateTime, default=datetime.now)                        # 创建时间
    status      = Column(Integer, default=1)                                    # 状态 ( 1:有效 0:无效 )

    username    = Column(String(50), nullable=False)                            # 用户名
    password    = Column(String(100), nullable=False)                           # 密码

    phone       = Column(String(50), default="")                                # 电话

    realname    = Column(String(50), default="")                                # 姓名 (真实姓名)
    email       = Column(String(100), default="")                               # 邮箱



'''
balance
'''
class model_balance(base):

    __tablename__ = 'balance'                                                   ### 账户余额 ###


    id          = Column(Integer, primary_key=True, autoincrement=True)         # 主键
    create_time = Column(DateTime, default=datetime.now)                        # 创建时间
    status      = Column(Integer, default=1)                                    # 状态 ( 1:有效 0:无效 )

    coin        = Column(String(20), nullable=False)                            # 币种
    amount      = Column(Float, default=0.0)                                    # 数量
    lock        = Column(Float, default=0.0)                                    # 冻结数量

    recharge_address    = Column(String(200), default="")                       # 充币地址
    recharge_memo       = Column(String(200), default="")                       # 充币备注

    withdraw_address    = Column(String(200), default="")                       # 提币地址
    withdraw_memo       = Column(String(200), default="")                       # 提币备注

    account_id  = Column(Integer, ForeignKey('account.id'))                     # 账户ID
    # account     = relationship("account", backref="balance-of-account")         # of account



'''
orders
'''
class model_orders(base):

    __tablename__ = 'orders'                                                     ### 委托 ###


    id          = Column(Integer, primary_key=True, autoincrement=True)         # 主键
    create_time = Column(DateTime, default=datetime.now)                        # 创建时间
    status      = Column(Integer, default=0)                                    # 状态 ( 0:无效 1:有效 2:已成交 3:撤单 )

    symbol              = Column(String(50), nullable=False)                    # 交易对
    price               = Column(Float, default=0.0)                            # 委托价格
    amount              = Column(Float, default=0.0)                            # 委托数量 ( +:buy -:sell )
    trade               = Column(Float, default=0.0)                            # 成交量 ( +:buy -:sell )

    account_id  = Column(Integer, ForeignKey('account.id'))                     # 账户ID
    # account     = relationship("account", backref="orders-of-account")          # of account



'''
trade
'''
class model_trade(base):

    __tablename__ = 'trade'                                                     ### 成交记录 ###

    id          = Column(Integer, primary_key=True, autoincrement=True)         # 主键
    create_time = Column(DateTime, default=datetime.now)                        # 创建时间
    status      = Column(Integer, default=1)                                    # 状态 ( 1:有效 0:无效 )

    symbol  = Column(String(50), nullable=False)                                # 交易对
    price   = Column(Float, default=0.0)                                        # 成交价格
    amount  = Column(Float, default=0.0)                                        # 成交数量

    order_buy_id = Column(Integer, ForeignKey('orders.id'))                     # 买入交易
    order_sell_id = Column(Integer, ForeignKey('orders.id'))                    # 卖出交易






if __name__ == '__main__':
    print("clear")
    model.clear()

    print("init")
    model.init()

    print("load_base_data")
    model.load_base_data()
